package com.wlz.util;

import org.apache.poi.openxml4j.opc.internal.FileHelper;

import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @author wlz
 * @date 2022-11-01  1:07 下午
 */
public class ZipUtils {

    /**
     * 解压文件
     */
    public static Boolean unZip(String zipFile, String path) {
        try {
            File pathfile = new File(path);
            if (!pathfile.exists()) {
                pathfile.mkdirs();
            }
            FileInputStream fis = new FileInputStream(new File(zipFile));
            ZipInputStream zis = new ZipInputStream(fis, Charset.forName("GBK"));
            ZipEntry zipEntry = null;
            while ((zipEntry = zis.getNextEntry()) != null) {
                File file = new File(pathfile.getAbsolutePath() + File.separator + zipEntry.getName());
                if (zipEntry.isDirectory()) {
                    file.mkdirs();
                } else {
                    File parentFile = FileHelper.getDirectory(file);
                    if (!parentFile.exists()) {
                        parentFile.mkdirs();
                    }
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    int len = -1;
                    byte[] buf = new byte[1024];
                    while ((len = zis.read(buf)) != -1) {
                        baos.write(buf, 0, len);
                    }
                    baos.close();
                    byte[] fileContent = baos.toByteArray();
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(fileContent);
                    fos.close();
                }
            }
            zis.close();
            fis.close();
            return true;
        } catch (Exception e) {
            System.out.println("解压文件异常 " + e );
            return false;
        }
    }

    /**
     * 解压
     *
     * @param zipFilePath 带解压文件
     * @param desDirectory 解压到的目录
     * @throws Exception
     */
    public static void unzip2(String zipFilePath, String desDirectory) throws Exception {

        File desDir = new File(desDirectory);
        if (!desDir.exists()) {
            boolean mkdirSuccess = desDir.mkdir();
            if (!mkdirSuccess) {
                throw new Exception("创建解压目标文件夹失败");
            }
        }
        // 读入流
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath));
        // 遍历每一个文件
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null) {
            if (zipEntry.isDirectory()) { // 文件夹
                String unzipFilePath = desDirectory + File.separator + zipEntry.getName();
                // 直接创建
                mkdir(new File(unzipFilePath));
            } else { // 文件
                String unzipFilePath = desDirectory + File.separator + zipEntry.getName();
                File file = new File(unzipFilePath);
                // 创建父目录
                mkdir(file.getParentFile());
                // 写出文件流
                BufferedOutputStream bufferedOutputStream =
                        new BufferedOutputStream(new FileOutputStream(unzipFilePath));
                byte[] bytes = new byte[1024];
                int readLen;
                while ((readLen = zipInputStream.read(bytes)) != -1) {
                    bufferedOutputStream.write(bytes, 0, readLen);
                }
                bufferedOutputStream.close();
            }
            zipInputStream.closeEntry();
            zipEntry = zipInputStream.getNextEntry();
        }
        zipInputStream.close();
    }

    // 如果父目录不存在则创建
    private static void mkdir(File file) {
        if (null == file || file.exists()) {
            return;
        }
        mkdir(file.getParentFile());
        file.mkdir();
    }

    public static void main(String[] args) throws Exception {
        long time = System.currentTimeMillis();
        String fullPath = "/Users/wlz/Desktop/HUMTEST_30-40.zip";
        String unPackageOriginalPath = fullPath.substring(0, fullPath.lastIndexOf("."));
        unzip2(fullPath, unPackageOriginalPath);
        System.out.println("time : " + (System.currentTimeMillis() - time));
    }

}
